package com.cmpe.group6.storyroad;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.cmpe.group6.storyroad.ListAdapters.MostRecentAdapter;
import com.cmpe.group6.storyroad.jsonClasses.SearchResultContainer;
import com.cmpe.group6.storyroad.jsonClasses.SearchResultObject;
import com.google.gson.Gson;

/** Activity showing the results of the search query.
 *
 */
public class SearchResults extends Activity {
	
	private Activity currentActivity;
	
	private ListView searchList;
	private ProgressBar progressBar;
	private SearchResultContainer searchResults;
	
	private final static ArrayList<String> story_titles = new ArrayList<String>();
	private final static ArrayList<String> story_ratings = new ArrayList<String>();
	private final static ArrayList<String> story_tags = new ArrayList<String>();
	private final static ArrayList<String> story_ids = new ArrayList<String>();
	
	private MostRecentAdapter listAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_results);
		String jsonResult = getIntent().getStringExtra("result");
		currentActivity = this;
		searchList = (ListView) findViewById(R.id.search_results_list);
		progressBar = (ProgressBar) findViewById(R.id.progressBar_search);
		Gson gson = new Gson();
		searchResults = gson.fromJson(jsonResult,
				SearchResultContainer.class);
		
		generateResultList();
		
		searchList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				String storyId = (String) searchList.getItemAtPosition(position);
				Intent readStoryIntent = new Intent(currentActivity,
						ReadStoryActivity.class);
				readStoryIntent.putExtra("story_id", storyId);
				startActivity(readStoryIntent);
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.home_button:
			Intent mainIntent = new Intent(this, MainActivity.class);
			startActivity(mainIntent);
			return true;
		case R.id.recommendations_button:
			Intent recomIntent = new Intent(this, RecommendationsActivity.class);
			startActivity(recomIntent);
			return true;
		case R.id.profile_button:
			Intent profileIntent = new Intent(this, ProfileActivity.class);
			startActivity(profileIntent);
			return true;
		case R.id.search_button:
			Intent searchIntent = new Intent(this, SearchActivity.class);
			startActivity(searchIntent);
			return true;
		case R.id.following_button:
			Intent followIntent = new Intent(this, FollowingActivity.class);
			startActivity(followIntent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	private void generateResultList(){
		
		story_titles.clear();
		story_ratings.clear();
		story_tags.clear();
		story_tags.clear();
		story_ids.clear();
		
		SearchResultObject[] resultArray = searchResults.getSearchResults();
		
		for (int i = 0; i < resultArray.length; i++) {
			SearchResultObject resultObject = resultArray[i];
			story_titles.add(resultObject.getTitle());
			story_ratings.add(String.valueOf(resultObject.getAvgRating()));
			story_tags.add(resultObject.getTags());
			story_ids.add(String.valueOf(resultObject.getS_id()));
		}

		listAdapter = new MostRecentAdapter(this, story_titles,
				story_ratings, story_tags, story_ids);
		
		searchList.setAdapter(listAdapter);
		
		progressBar.setVisibility(View.GONE);
	}

}
